package com.shark.stock.model
{
	import com.shark.stock.events.StockEvent;
	import com.shark.stock.vos.Article;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import flashx.textLayout.formats.Float;
	
	import mx.collections.ArrayCollection;
	
	// Esta es la lista de eventos que dispara la clase...
	[Event(name="SAVE_ARTICLE", type="com.shark.stock.events.StockEvent")]
	[Event(name="SAVE_DONE", type="com.shark.stock.events.StockEvent")]

	public class NewArticleModel extends EventDispatcher
	{
		[Bindable]
		public var article:Article;
		
		[Bindable]
		public var categories:ArrayCollection;
		
		[Bindable]
		public var nameError:String ="";
		
		private var dispatcher:IEventDispatcher;
		
		public function NewArticleModel ( dispatcher:IEventDispatcher )
		{
			super();
			this.dispatcher = dispatcher;
			nameError ="";
		}		
		
		public function save():Boolean
		{
			nameError ="";
			if(article.name == ""){
				nameError = "Please, give a name";
				return false;
			}else{
				var evt:StockEvent = new StockEvent( StockEvent.SAVE_ARTICLE );
				evt.article = article;
				return dispatcher.dispatchEvent( evt );
			}
		}
		
		public function  saveResult( result:Boolean ):void
		{
			if( result )
			{
				dispatchEvent( new StockEvent( StockEvent.SAVE_DONE ) );
			}
		}
		
			
			
	}
}